<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/wang-cheng/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wang-cheng/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wang-cheng/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wang-cheng/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wang-cheng/css/main.css">


<link rel="stylesheet" href="/wang-cheng/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"cheng-wang123.gitee.io","root":"/wang-cheng/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="算法基础图的存储方式 邻接表  只写直接邻居，间接邻居不管  邻接矩阵  正方形的矩阵，适合于带有权值的图，有向图、无向图都能表示 对于两个边不相连的点，用无穷表示 对于两个边相连的点，用他们的权值表示 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535">
<meta property="og:type" content="article">
<meta property="og:title" content="算法笔记(二)">
<meta property="og:url" content="https://cheng-wang123.gitee.io/wang-cheng/2022/04/01/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%BA%8C/index.html">
<meta property="og:site_name" content="诚の博客">
<meta property="og:description" content="算法基础图的存储方式 邻接表  只写直接邻居，间接邻居不管  邻接矩阵  正方形的矩阵，适合于带有权值的图，有向图、无向图都能表示 对于两个边不相连的点，用无穷表示 对于两个边相连的点，用他们的权值表示 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-04-01T08:21:35.000Z">
<meta property="article:modified_time" content="2022-04-02T13:18:22.380Z">
<meta property="article:author" content="王诚">
<meta property="article:tag" content="左神">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://cheng-wang123.gitee.io/wang-cheng/2022/04/01/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%BA%8C/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>算法笔记(二) | 诚の博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/wang-cheng/atom.xml" title="诚の博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wang-cheng/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">诚の博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录学习中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/wang-cheng/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/wang-cheng/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/wang-cheng/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/wang-cheng/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://cheng-wang123.gitee.io/wang-cheng/2022/04/01/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%BA%8C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wang-cheng/images/avatar.gif">
      <meta itemprop="name" content="王诚">
      <meta itemprop="description" content="命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="诚の博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          算法笔记(二)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-01 16:21:35" itemprop="dateCreated datePublished" datetime="2022-04-01T16:21:35+08:00">2022-04-01</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-02 21:18:22" itemprop="dateModified" datetime="2022-04-02T21:18:22+08:00">2022-04-02</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="算法基础"><a href="#算法基础" class="headerlink" title="算法基础"></a><strong>算法基础</strong></h1><h2 id="图的存储方式"><a href="#图的存储方式" class="headerlink" title="图的存储方式"></a><strong>图的存储方式</strong></h2><ol>
<li>邻接表</li>
</ol>
<p>只写直接邻居，间接邻居不管</p>
<ol start="2">
<li>邻接矩阵</li>
</ol>
<p>正方形的矩阵，适合于带有权值的图，有向图、无向图都能表示</p>
<p>对于两个边不相连的点，用无穷表示</p>
<p>对于两个边相连的点，用他们的权值表示</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">public</span> <span class="keyword">class</span> Graph&#123;</span><br><span class="line">    <span class="built_in">public</span> HashMap&lt;<span class="type">Integer</span>,Node&gt; nodes;</span><br><span class="line">    <span class="built_in">public</span> HashSet&lt;Edge&gt; edges;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">public</span> Graph()&#123;</span><br><span class="line">        nodes = <span class="built_in">new</span> HashMap&lt;&gt;();</span><br><span class="line">        edges = <span class="built_in">new</span> HashSet&lt;&gt;();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> <span class="keyword">class</span> Node&#123;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> <span class="keyword">value</span>;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> <span class="keyword">in</span>;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> <span class="keyword">out</span>;</span><br><span class="line">    <span class="built_in">public</span> ArrayList&lt;Node&gt; nexts;</span><br><span class="line">    <span class="built_in">public</span> ArrayList&lt;Edge&gt; edges;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">public</span> Node(<span class="type">int</span> <span class="keyword">value</span>)&#123;</span><br><span class="line">        this.<span class="keyword">value</span> = <span class="keyword">value</span>;</span><br><span class="line">        <span class="keyword">in</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">out</span> = <span class="number">0</span>;</span><br><span class="line">        nexts=<span class="built_in">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        edges = <span class="built_in">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> <span class="keyword">class</span> Edge&#123;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> weight;</span><br><span class="line">    <span class="built_in">public</span> Node <span class="keyword">from</span>;</span><br><span class="line">    <span class="built_in">public</span> Node <span class="keyword">to</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">public</span> Edge(<span class="type">int</span> weight,Node <span class="keyword">from</span>,Node <span class="keyword">to</span>)&#123;</span><br><span class="line">        this.weight = weight;</span><br><span class="line">        this.<span class="keyword">from</span> = <span class="keyword">from</span>;</span><br><span class="line">        this.<span class="keyword">to</span> <span class="keyword">to</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> calss GraphGenerator&#123;</span><br><span class="line">    <span class="built_in">public</span> static Graph createGraph(<span class="type">Integer</span>[][] matrix)&#123;</span><br><span class="line">        Graph graph = <span class="built_in">new</span> Graph();</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; matrix.length;i++)&#123;</span><br><span class="line">            <span class="type">Integer</span> <span class="keyword">from</span> = matrix[i][<span class="number">0</span>];</span><br><span class="line">            <span class="type">Integer</span> <span class="keyword">to</span> = matrix[i][<span class="number">1</span>];</span><br><span class="line">            <span class="type">Integer</span> weight = matrix[i][<span class="number">2</span>];</span><br><span class="line">            <span class="keyword">if</span>(!graph.nodes.containsKey(<span class="keyword">from</span>))&#123;</span><br><span class="line">                graph.nodes.put(<span class="keyword">from</span>,<span class="built_in">new</span> Node(<span class="keyword">from</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(!graph.nodes.containsKey(<span class="keyword">to</span>))&#123;</span><br><span class="line">                graph.nodes.put(<span class="keyword">to</span>,<span class="built_in">new</span> Node(<span class="keyword">to</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            Node fromNode = graph.nodes.<span class="keyword">get</span>(form);</span><br><span class="line">            Node toNode = graph.nodes.<span class="keyword">get</span>(<span class="keyword">to</span>);</span><br><span class="line">            Edge newEdge = <span class="built_in">new</span> Edge(weight,fromNode,toNode);</span><br><span class="line">            fromNode.nexts.<span class="keyword">add</span>(toNode);</span><br><span class="line">            fromNode.<span class="keyword">out</span>++;</span><br><span class="line">            toNode.<span class="keyword">in</span>++;</span><br><span class="line">            fromNode.edges.<span class="keyword">add</span>(newEdge);</span><br><span class="line">            graph.edges.<span class="keyword">add</span>(newEdge);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> graph;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="图的宽度优先遍历"><a href="#图的宽度优先遍历" class="headerlink" title="图的宽度优先遍历"></a><strong>图的宽度优先遍历</strong></h2><ol>
<li><p>利用队列实现</p>
</li>
<li><p>从源节点开始依次按照宽度进队列，然后弹出</p>
</li>
<li><p>每弹出一个点，把该节点所有没有进过队列的邻接点放入队列</p>
</li>
<li><p>直到队列变空</p>
</li>
</ol>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">dfs</span>(<span class="params">Node node</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(node == <span class="literal">null</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    Queue&lt;Node&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();<span class="comment">//图中的点进队列</span></span><br><span class="line">    HashSet&lt;Node&gt; <span class="keyword">set</span> = <span class="keyword">new</span> HashSet&lt;&gt;();<span class="comment">//保证图中的点不要重复进队列</span></span><br><span class="line"></span><br><span class="line">    queue.<span class="keyword">add</span>(node);</span><br><span class="line">    <span class="keyword">set</span>.<span class="keyword">add</span>(node);</span><br><span class="line">    <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">        Node cur = queue.poll();</span><br><span class="line">        System.<span class="keyword">out</span>.println(cur.<span class="keyword">value</span>);</span><br><span class="line">        <span class="keyword">for</span>(Node next : cur.nexts)&#123;</span><br><span class="line">            <span class="keyword">if</span>(!<span class="keyword">set</span>.contains(next))&#123;</span><br><span class="line">                <span class="keyword">set</span>.<span class="keyword">add</span>(next);</span><br><span class="line">                queue.<span class="keyword">add</span>(next);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="图的广度优先遍历"><a href="#图的广度优先遍历" class="headerlink" title="图的广度优先遍历"></a><strong>图的广度优先遍历</strong></h2><ol>
<li><p>利用栈实现</p>
</li>
<li><p>从源节点开始把节点按照深度放入栈，然后弹出</p>
</li>
<li><p>每弹出一个点，把该节点下一个没有进过栈的邻接点放入栈</p>
</li>
<li><p>直到栈变空</p>
</li>
</ol>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="type">static</span> <span class="type">void</span> <span class="title">dfs</span><span class="params">(Node node)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(node == null)&#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    Stack&lt;Node&gt; stack = <span class="keyword">new</span> Stack&lt;&gt;();</span><br><span class="line">    HashSet&lt;Node&gt; set = <span class="keyword">new</span> HashSet&lt;&gt;();</span><br><span class="line">    stack.<span class="built_in">add</span>(node);</span><br><span class="line">    set.<span class="built_in">add</span>(node);</span><br><span class="line">    System.out.<span class="built_in">println</span>(node.value);</span><br><span class="line">    <span class="keyword">while</span>(!stack.<span class="built_in">isEmpty</span>())&#123;</span><br><span class="line">        Node cur = stack.<span class="built_in">pop</span>();<span class="comment">//把当前点弹出</span></span><br><span class="line">        <span class="keyword">for</span>(Node node : cur.nexts)&#123;</span><br><span class="line">            <span class="keyword">if</span>(!set.<span class="built_in">contains</span>(next))&#123;</span><br><span class="line">                stack.<span class="built_in">push</span>(cur);<span class="comment">//当前点再放入栈</span></span><br><span class="line">                stack.<span class="built_in">push</span>(next);<span class="comment">//当前点的下一个点也放入栈</span></span><br><span class="line">                set.<span class="built_in">add</span>(next);</span><br><span class="line">                System.out.<span class="built_in">println</span>(next.value);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="拓扑排序算法"><a href="#拓扑排序算法" class="headerlink" title="拓扑排序算法"></a><strong>拓扑排序算法</strong></h2><p>适用范围：要求有向图，且有入度为0的节点，且没有环</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public static List<span class="tag">&lt;Node&gt;</span> sortedTopology(Graph graph)&#123;</span><br><span class="line">    //key  某一个<span class="keyword">node</span></span><br><span class="line">    <span class="title">//value</span>   剩余的入度</span><br><span class="line">    HashMap<span class="tag">&lt;Node,Integer&gt;</span> inMap = new HashMap<span class="tag">&lt;&gt;</span>();</span><br><span class="line">    //入度为<span class="number">0</span>的点，才能进这个队列</span><br><span class="line">    Queue<span class="tag">&lt;Node&gt;</span> zeroInQueue = new LinkedList<span class="tag">&lt;&gt;</span>();</span><br><span class="line">    for(<span class="keyword">Node</span> <span class="title">node</span> : garph.nodes.values())&#123;</span><br><span class="line">        inMap.put(<span class="keyword">node</span><span class="title">,node</span>.<span class="keyword">in</span>);</span><br><span class="line">        if(node.<span class="keyword">in</span> == <span class="number">0</span>)&#123;</span><br><span class="line">            zeroInQueue.add(<span class="keyword">node</span><span class="title">);</span></span><br><span class="line"><span class="title">        &#125;</span></span><br><span class="line"><span class="title">    &#125;</span></span><br><span class="line"><span class="title">    List</span><span class="tag">&lt;Node&gt;</span> result = new ArrayList<span class="tag">&lt;&gt;</span>();</span><br><span class="line">    while(!zeroInQueue.isEmpty())&#123;</span><br><span class="line">        <span class="keyword">Node</span> <span class="title">cur</span> = zeroInQueue.poll();</span><br><span class="line">        result.add(cur);</span><br><span class="line">        for(<span class="keyword">Node</span> <span class="title">next</span> : cur.nexts)&#123;</span><br><span class="line">            inMap.put(next,inMap.get(next) - <span class="number">1</span>);</span><br><span class="line">            if(inMap.get(next) == <span class="number">0</span>)&#123;</span><br><span class="line">                zeroInQueue.add(next);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Kruskal算法"><a href="#Kruskal算法" class="headerlink" title="Kruskal算法"></a><strong>Kruskal算法</strong></h2><p>适用范围：无向图</p>
<p>思想:</p>
<ol>
<li><p>从无向图中，找出最小权值的边，接下来再画出这个图的没有边的图，试着把这条边加上后，看会不会形成环，如果不会，则保留这个边</p>
</li>
<li><p>继续找最小权值的边，如果加上这个边后，会形成环，则将这条边忽略</p>
</li>
<li><p>继续进行上面两步操作</p>
</li>
</ol>
<h2 id="何为前缀树，如何生成前缀树"><a href="#何为前缀树，如何生成前缀树" class="headerlink" title="何为前缀树，如何生成前缀树"></a><strong>何为前缀树，如何生成前缀树</strong></h2><p>例如：一个字符串类型的数组arr1，另一个字符串类型的数组arr2。arr2中有哪些字符，是arr1中出现的？请打印。arr2中有哪些字符，是作为arr1中某个字符串前缀出现的，请打印。arr2中有哪些字符，是作为arr1中某个字符串前缀出现的？请打印arr2中出现次数最大的前缀。</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">public</span> static <span class="keyword">class</span> TrieNode&#123;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> pass;</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> <span class="keyword">end</span>;</span><br><span class="line">    <span class="built_in">public</span> TrieNode[] nexts;</span><br><span class="line">    <span class="built_in">public</span> TrieNode()&#123;</span><br><span class="line">        pass = <span class="number">0</span>;</span><br><span class="line">        end = <span class="number">0</span>;</span><br><span class="line">        nexts = <span class="built_in">new</span> TrieNode[<span class="number">26</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> static <span class="keyword">class</span> Trie&#123;</span><br><span class="line">    private TrieNode root;</span><br><span class="line">    <span class="built_in">public</span> Trie()&#123;</span><br><span class="line">        root = <span class="built_in">new</span> TrieNode();</span><br><span class="line">    &#125;</span><br><span class="line">    //加入字符串</span><br><span class="line">    <span class="built_in">public</span> <span class="type">void</span> <span class="keyword">insert</span>(String word)&#123;</span><br><span class="line">        <span class="keyword">if</span>(word == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">char</span>[] chs = word.toCharArray();</span><br><span class="line">        TrieNode node = root;</span><br><span class="line">        node.pass++;</span><br><span class="line">        <span class="type">int</span> <span class="keyword">index</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i&lt; chs.length; i++)&#123;</span><br><span class="line">            <span class="keyword">index</span> = chs[i] - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">            <span class="keyword">if</span>(node.nexts[<span class="keyword">index</span>] == <span class="keyword">null</span>)&#123;</span><br><span class="line">                node.nexts[<span class="keyword">index</span>] = <span class="built_in">new</span> TrieNode();</span><br><span class="line">            &#125;</span><br><span class="line">            node = node.nexts[<span class="keyword">index</span>];</span><br><span class="line">            node.<span class="keyword">end</span>++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    //word这个单词之前加入过几次</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> <span class="keyword">search</span>(String word)&#123;</span><br><span class="line">        <span class="keyword">if</span>(word == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">char</span>[] chs = word.toCharArray();</span><br><span class="line">        TrieNode node = root;</span><br><span class="line">        <span class="type">int</span> <span class="keyword">index</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; chs.length;i++)&#123;</span><br><span class="line">            <span class="keyword">index</span> = chs[i] - <span class="string">&#x27;a&#x27;</span>;//字符变量<span class="string">&#x27;a&#x27;</span>在做算数运算的时候，会被当成整型变量处理。<span class="string">&#x27;a&#x27;</span> = <span class="number">97</span>,这里用一个字符-<span class="string">&#x27;a&#x27;</span>,正好是TrieNode申请的数组的长度。数组的每一个下标代表一个字母。这里 字符 - <span class="string">&#x27;a&#x27;</span> 的结果正好是数组中这个字符的下标。</span><br><span class="line">            <span class="keyword">if</span>(node.nexts[<span class="keyword">index</span>] == <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            node = node.nexts[<span class="keyword">index</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> node.<span class="keyword">end</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    //所有加入的字符串中，有几个是以pre这个字符串作为前缀的</span><br><span class="line">    <span class="built_in">public</span> <span class="type">int</span> prefixNumber(String pre)&#123;</span><br><span class="line">        <span class="keyword">if</span>(pre == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">char</span>[] chs = pre.toCharArray();</span><br><span class="line">        TrieNode node = root;</span><br><span class="line">        <span class="type">int</span> <span class="keyword">index</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; chs.length;i++)&#123;</span><br><span class="line">            <span class="keyword">index</span> = chs[i] - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">            <span class="keyword">if</span>(node.nexts[<span class="keyword">index</span>] == <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            node = node.nexts[<span class="keyword">index</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> node.pass;</span><br><span class="line">    &#125;</span><br><span class="line">    //删除</span><br><span class="line">    <span class="built_in">public</span> <span class="type">void</span> <span class="keyword">delete</span>(String word)&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">search</span>(word) != <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="type">char</span>[] chs = word.toCharArray();</span><br><span class="line">            TrieNode node = root;</span><br><span class="line">            node.pass<span class="comment">--;</span></span><br><span class="line">            <span class="type">int</span> <span class="keyword">index</span> = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; chs.length; i++)&#123;</span><br><span class="line">                <span class="keyword">index</span> = chs[i] - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">                <span class="keyword">if</span>(<span class="comment">--node.nexts[index].pass == 0)&#123;</span></span><br><span class="line">                    node.nexts[<span class="keyword">index</span>] = <span class="keyword">null</span>;</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                node = node.nexts[<span class="keyword">index</span>];</span><br><span class="line">            &#125;</span><br><span class="line">            node.<span class="keyword">end</span><span class="comment">--;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


















































    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/wang-cheng/tags/%E5%B7%A6%E7%A5%9E/" rel="tag"># 左神</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/03/31/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%B8%80/" rel="prev" title="算法笔记(一)">
      <i class="fa fa-chevron-left"></i> 算法笔记(一)
    </a></div>
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/04/02/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%B8%89/" rel="next" title="算法笔记(三)">
      算法笔记(三) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E5%9F%BA%E7%A1%80"><span class="nav-number">1.</span> <span class="nav-text">算法基础</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E5%AD%98%E5%82%A8%E6%96%B9%E5%BC%8F"><span class="nav-number">1.1.</span> <span class="nav-text">图的存储方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86"><span class="nav-number">1.2.</span> <span class="nav-text">图的宽度优先遍历</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86"><span class="nav-number">1.3.</span> <span class="nav-text">图的广度优先遍历</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95"><span class="nav-number">1.4.</span> <span class="nav-text">拓扑排序算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Kruskal%E7%AE%97%E6%B3%95"><span class="nav-number">1.5.</span> <span class="nav-text">Kruskal算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%95%E4%B8%BA%E5%89%8D%E7%BC%80%E6%A0%91%EF%BC%8C%E5%A6%82%E4%BD%95%E7%94%9F%E6%88%90%E5%89%8D%E7%BC%80%E6%A0%91"><span class="nav-number">1.6.</span> <span class="nav-text">何为前缀树，如何生成前缀树</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">王诚</p>
  <div class="site-description" itemprop="description">命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wang-cheng/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/wang-cheng/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王诚</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/wang-cheng/lib/anime.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.ui.min.js"></script>

<script src="/wang-cheng/js/utils.js"></script>

<script src="/wang-cheng/js/motion.js"></script>


<script src="/wang-cheng/js/schemes/pisces.js"></script>


<script src="/wang-cheng/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
